package com.oa.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.oa.back.ResponseBack;
import com.oa.common.base.Page;
import com.oa.common.base.Pageable;
import com.oa.common.dto.DepartmentDto;
import com.oa.common.enums.CodeStatus;
import com.oa.common.eums.EntityStatus;
import com.oa.common.vo.DepartmentVo;
import com.oa.dao.BaseDao;
import com.oa.dao.DepartmentDao;
import com.oa.entities.Department;
import com.oa.service.DepartmentService;
import com.oa.service.UndeleteServiceImpl;
import com.oa.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class DepartmentServiceImpl extends UndeleteServiceImpl<Long,Department> implements DepartmentService {

    /** 日誌輸出類 **/
    private static final Logger logger= LoggerFactory.getLogger(DepartmentServiceImpl.class);

    @Autowired
    private DepartmentDao departmentDao;

    @Autowired
    private UserService userService;

    @Override
    protected BaseDao<Long, Department> getSelfDao() {
        return departmentDao;
    }

    @Override
    public void setClass(Class<Department> clazz) {

    }

    @Override
    public List<Department> findParent() {
        return departmentDao.findParent();
    }

    @Override
    @Transactional
    public ResponseBack<String> submit(String params) {
        if (StringUtils.isEmpty(params)){
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        try{
            DepartmentDto departmentDto= JSONObject.parseObject(params,DepartmentDto.class);
            if (departmentDto==null){
                return ResponseBack.fial(CodeStatus.PASSWORD_FAIL.getResponseStatus());
            }
            if (StringUtils.isEmpty(departmentDto.getName())){
                return ResponseBack.fial(CodeStatus.DEPARTMENT_NAME_NOTNULL.getResponseStatus());
            }
            if(departmentDao.validate(departmentDto.getName(),null)){
                return ResponseBack.fial(CodeStatus.DEPARTMENT_EXIST.getResponseStatus());
            }
            Department department= new Department();
            department.setName(departmentDto.getName());
            department.setDepDesc(departmentDto.getDecs());
            int level= department.getParent()!=null?department.getParent().getDepLevel()+1:1;
            department.setDepLevel(level);
            if (departmentDto.getDepartmentId()!=null){
                Department pdepartment= get(departmentDto.getDepartmentId());
                if (pdepartment== null){
                    return ResponseBack.fial(CodeStatus.DEPARTMENT_UNDERFUND.getResponseStatus());
                }
                department.setParent(pdepartment);
            }
            department.setParentId(departmentDto.getDepartmentId());
            save(department);
        }catch (RuntimeException ex){
            logger.error("提交部門的異常:",ex);
            return ResponseBack.fial(CodeStatus.PARAMS_FAIL.getResponseStatus());
        }
        return ResponseBack.success(null);
    }

    @Override
    @Transactional
    public ResponseBack<String> update(String params) {
        if (StringUtils.isEmpty(params)){
            return ResponseBack.fial(CodeStatus.PASSWORD_FAIL.getResponseStatus());
        }
        try{
            DepartmentDto dto= JSONObject.parseObject(params,DepartmentDto.class);
            if (dto==null){
                return ResponseBack.fial(CodeStatus.PASSWORD_FAIL.getResponseStatus());
            }
            Department department= get(dto.getId());
            if (department==null){
                return ResponseBack.fial(CodeStatus.DEPARTMENT_UNDERFUND.getResponseStatus());
            }
            if (StringUtils.isEmpty(dto.getName())){
                return ResponseBack.fial(CodeStatus.DEPARTMENT_NAME_NOTNULL.getResponseStatus());
            }
            if (departmentDao.validate(dto.getName(),dto.getId())){
                return ResponseBack.fial(CodeStatus.DEPARTMENT_EXIST.getResponseStatus());
            }
            department.setName(dto.getName());
            department.setDepDesc(dto.getDecs());
            update(department);
        }catch (RuntimeException ex){
            return ResponseBack.fial(CodeStatus.PASSWORD_FAIL.getResponseStatus());
        }
        return ResponseBack.success(null);
    }

    @Override
    public Boolean validate(String name, Long id) {
        return departmentDao.validate(name, id);
    }

    @Override
    @Transactional
    public ResponseBack<String> deleteDepartment(Long id) {
        Department department= get(id);
        if (department==null){
            return ResponseBack.fial(CodeStatus.DEPARTMENT_UNDERFUND.getResponseStatus());
        }
        List<Department> departments=departmentDao.findByParentId(id);
        if (departments!=null && departments.size()>0){
            return ResponseBack.fial(CodeStatus.DEPARTMENT_HAS_USER.getResponseStatus());
        }
        if (userService.validateByDepartment(id)){
            return ResponseBack.fial(CodeStatus.DEPARTMENT_HAS_CHILD.getResponseStatus());
        }
        disable(department);
        return ResponseBack.success(null);
    }

    @Override
    public ResponseBack<List<DepartmentVo>> queryAll() {
        List<Department> departments= findByStatus(EntityStatus.ENTITY_EABLE.getCode(),null);
        List<DepartmentVo> vos= new ArrayList<>();
        for (Department department: departments){
            vos.add(new DepartmentVo(department));
        }
        return ResponseBack.success(vos);
    }

    @Override
    public Page<Department> findPage(Pageable pageable, String key) {
        return departmentDao.findPage(pageable,key);
    }
}
